import {
    ADD_COMMENT,
    DELETE_COMMENT,
} from '../action/action.js';

/**
 * 设计 state {
 *      comments:[
 *          username:'jack',
 *          content:'hello world'
 *      ]
 * }
 * @param {obj} state
 * @param {obj} action
 */

export default function (state, action) {
    switch (action.type) {
        case ADD_COMMENT:
            return {
                comments: [
                    ...state.comments,
                    action.comments,
                ],
            };
        case DELETE_COMMENT:
            return {
                comments: [
                    ...state.comments.slice(0, action.index),
                    ...state.comments.slice(action.index + 1),
                ],
            };
        default:
            return state;
    }
}
